<template>
  <div>
    <!-- 药品维护 -->
    药品名称:<el-input
      style="width: 150px"
      v-model="ypmc"
      placeholder="请输入药品名称"
    ></el-input>
    药品分类:<el-select
      style="width: 90px"
      v-model="value"
      placeholder="请选择药品类型"
    >
      <el-option
        v-for="item in options"
        :key="item.value"
        :label="item.label"
        :value="item.value"
      >
      </el-option>
    </el-select>
    <el-button
      type="primary"
      icon="el-icon-search"
      @click="chaxun()"
    ></el-button>
    <!-- <el-button type="primary" @click="chaxun()">查询</el-button> -->
    <!-- <el-button @click="tjypxx()">添加药品信息</el-button> -->
    <el-button
      type="primary"
      @click="dialogVisible = true"
      style="margin-left: 900px"
      >导入药品</el-button
    >
       
    <el-dialog title="导入药品信息" :visible.sync="dialogVisible" width="50%">
      <span>药品名称:</span>
      <el-input
        v-model="ypmcc"
        placeholder="请输入药品名称"
        style="width: 300px"
      ></el-input>
      <span>药品分类:</span>
      <el-select
        style="width: 90px"
        v-model="value"
        placeholder="请选择药品类型"
      >
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
      <el-button type="primary" icon="el-icon-search" @click="chaxun1()"></el-button>

      <el-table
        :data="paginatedPurchaseList"
        style="width: 120%; margin-top: 15px"
        @selection-change="ChangeRestoratives"
      >
        <el-table-column prop="medicinesNumber" label="编号" type="selection">
        </el-table-column>
        <el-table-column prop="medicinesName" label="药品名称">
        </el-table-column>
        <el-table-column prop="medicinesClass" label="药品分类">
        </el-table-column>
        <!-- <el-table-column prop="inventoryNum" label="数量">
        </el-table-column> -->
        <el-table-column prop="unit" label="单位"> </el-table-column>
        <el-table-column prop="conversion" label="规格"> </el-table-column>
      </el-table>
      <el-pagination
        @current-change="handleCurrentChangev"
        :current-page.sync="currentPagev"
        :page-size="pageSizev"
        layout="total, prev, pager, next"
        :total="total"
      ></el-pagination>

      <!-- <el-button type="primary">查询</el-button> -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="importMed"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <el-table
      :data="
        tableDataa.slice((currentPage - 1) * pageSize, currentPage * pageSize)
      "
      style="width: 120%; margin-top: 30px"
    >
      <el-table-column prop="medicinesNumber" label="编号"> </el-table-column>
      <el-table-column prop="medicinesName" label="药品名称"> </el-table-column>
      <el-table-column prop="medicinesClass" label="药品分类">
      </el-table-column>
      <!-- <el-table-column prop="inventoryNum" label="数量">
      </el-table-column> -->
      <el-table-column prop="unit" label="单位"> </el-table-column>
      <el-table-column prop="conversion" label="规格"> </el-table-column>
      <el-table-column prop="producter" label="生产厂家"> </el-table-column>
      <el-table-column prop="createTime" label="创建日期">
        <template slot-scope="scope">
    {{ formatDate(scope.row.createTime, scope.row) }}
  </template>   
      </el-table-column>
      <el-table-column prop="caozuo" label="操作" width="250">
        <!-- <el-button type="danger" id="bu" icon="el-icon-delete"></el-button> -->
        <template slot-scope="sc">
          <!-- 表格的当前行 -->
          <el-button
            type="danger"
            @click="shanchu(sc.row.hmrId)"
            style="width: 65px"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-size="pageSize"
        layout="total, prev, pager, next"
        :total="tableDataa.length"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
  import { get } from '@/utils/request'
  import axios from 'axios'
  import dayjs from 'dayjs'
  export default {
    name: 'Longin',
    data() {
      return {
        ypmc: '',
        ypmcc:'',
        medicinesId: '',
        dialogVisible: false,
        formData: [],
        tableDataa: [],
        medInfo: [],
        checkedMed: [],
        options: [
          {
            value: '选项1',
            label: '中草药',
          },
        ],

        value: '',
        //分页=当前页
        currentPage: 1,
        currentPagev: 1,
        pageSize: 8,
        pageSizev: 7,
        total: 0,
      }
    },
    computed: {
      paginatedPurchaseList() {
        const start = (this.currentPagev - 1) * this.pageSizev
        const end = start + this.pageSizev
        return this.medInfo.slice(start, end)
      },
    },
    methods: {
      formatDate(row){
      return dayjs(row).format('YYYY/MM/DD')
},
      ChangeRestoratives(val) {
        let x = []
        val.forEach((element) => {
          x.push(element.medicinesId)
        })
        this.checkedMed = x
        console.log(x)
      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`)
        this.currentPage = val
      },
      handleCurrentChangev(val) {
        this.currentPagev = val
      },
      /**
       * 查询药品名称
       */
      chaxun() {
        axios({
          url: 'http://127.0.0.1:8686/api/xianshi',
          method: 'get',
          params: { medicinesName: this.ypmc },
        }).then((re) => {
          this.tableDataa = re.data.data
        })
      },
      /**
       * 药品删除
       */
      shanchu(hmrId) {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(() => {
                  axios({
                url: 'http://127.0.0.1:8686/api/DzmHisMedicinesshanchu',
                method: 'delete',
                params: { hmrId: hmrId },
                  }).then((re) => {
                   console.log(re.data.data)
                if (re.data.data==true) {
                      this.$message({
                        type: 'success',
                        message:  re.data.msg,
                      })
                  this.one()
                } else {
                  this.$message({
                        type: 'warning',
                        message: re.data.msg,
                      })
                }
              })
           
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除',
            })
          })
        let formData = new FormData()
        formData.append('medicinesId', medicinesId)

       
      },

      one() {
        axios({
          url: 'http://127.0.0.1:8686/api/xianshi',
          method: 'get',
        }).then((re) => {
          this.tableDataa = re.data.data
          console.log(re)
        })
      },
      /**
       * 显示导入药品信息
       */
      getMedicalInfo() {
        axios({
          url: 'http://127.0.0.1:8686/api/medical_info',
          method: 'get',
        }).then((res) => {
          this.medInfo = res.data.data
          this.total = res.data.data.length
        })
      },
      importMed() {
        this.dialogVisible = false
        /**
         * 导入药品
         */
        axios({
          url: 'http://127.0.0.1:8686/api/modify_h_med',
          method: 'post',
          data: this.checkedMed,
        }).then((res) => {
          alert("导入成功")
          this.one()
          console.log(res)
        })
      },
      /**
       * 导入药品信息查询
       */
       chaxun1(){
        axios({
          url:"http://127.0.0.1:8686/api/DzmHisMedicinesdaoruchaxun",
          method:"get",
          params:{medicinesName: this.ypmcc},
        }).then((re)=>{
          this.medInfo=re.data
          this.total = re.data.length
          // this.total = res.data.data.length
        })
       }
    },
    mounted() {
      /**
       * 显示页面
       */
      // axios({
      //   url: "http://127.0.0.1:8686/api/xianshi",
      //   method: "get"
      // }).then((re) => {
      //   this.tableDataa = re.data.data
      //   console.log(re)

      // })
      this.one()
      this.getMedicalInfo()
    },
  }
</script>

<style></style>
